package cn.com.dhcc.app.pub.module.authmgr.controller;

import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.dhcc.app.core.annotation.CoreMenuCode;
import cn.com.dhcc.app.core.annotation.CoreMenuCode.CORE_LEAF_MENU;
import cn.com.dhcc.app.core.base.BaseController;
import cn.com.dhcc.app.core.base.log.LogType;
import cn.com.dhcc.app.core.component.db.orm.WhereCondition;
import cn.com.dhcc.app.core.component.db.orm.valuebean.LikeValue;
import cn.com.dhcc.app.core.component.log.Log;
import cn.com.dhcc.app.core.component.pager.Pager;
import cn.com.dhcc.app.core.component.pager.PagerData;
import cn.com.dhcc.app.core.exception.NiceException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.tag.CommonStaticCode.ENTITY_STATUS;
import cn.com.dhcc.app.core.tag.CommonStaticCode.OPERATION_TYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.SUCCESSFAIL;
import cn.com.dhcc.app.core.util.StrUtil;
import cn.com.dhcc.app.pub.module.authmgr.service.AuthRightMgrService;
import cn.com.dhcc.app.pub.module.authmgr.vo.AuthRight;
/**
 * 权限管理
 * @author qiao
 * @2015-9-17 @下午5:47:58
 */
@Controller
@RequestMapping("pub/sysmgr/authmgr")
@CoreMenuCode(belongToMenu=CORE_LEAF_MENU.AUTH_RIGHT_MGR)
public class RightMgrController extends BaseController{

	@Autowired
	private AuthRightMgrService authRightMgrService;
	
	Logger logger = Log.getLogger(LogType.AUTH_MGR);
	/**
	 * 
	 * @param model
	 * @param pager
	 * @param rightName
	 * @param order
	 * @param orderName
	 * @return
	 */
	@RequestMapping("right_list")
	public String rightList(Model model,Pager pager, String rightName, String remark, String type, String order,String orderName){
		try{
			WhereCondition searchData=new WhereCondition();
			searchData.where1Eq1();
			searchData.andLike("right_name", LikeValue.roundLike(StrUtil.isEmpty(rightName) ? rightName : rightName.trim()));
			searchData.andLike("remark", LikeValue.roundLike(StrUtil.isEmpty(remark) ? remark : remark.trim()));
			searchData.andEq("type", type);
			searchData.andNotEq("status", ENTITY_STATUS.DELETE.getCode());
			if(StrUtil.isNotBank(orderName)){
				searchData.orderBy(orderName+" "+order);
			}
			PagerData<AuthRight> pagerData = authRightMgrService.listRight(pager,searchData);
			model.addAttribute("rights", pagerData.getDatas());
			setTotoal(model, pagerData.getTotal());
		} catch (ServiceException e) {
			return toAlertPage(model, e.getMessage());
		}
		return "pub/sysmgr/authmgr/auth_right_list";
	}
	/**
	 * 查看角色
	 * @param model
	 * @param Id
	 * @return
	 */
	@RequestMapping("get_right")
	public String getRight(Model model, String id){
		try{
			if(!StringUtils.hasText(id))return toAlertPage(model, "权限id不能为空");
			AuthRight authRight = authRightMgrService.getRight(id);
			model.addAttribute("authRight", authRight);
			model.addAttribute("resourceData", authRightMgrService.getMenuByRightId(id,OPERATION_TYPE.GET));
		} catch (ServiceException e) {
			return toAlertPage( model, "获取权限失败");
		}
		return "pub/sysmgr/authmgr/auth_right_detail";
	}
	/**
	 * 物理删除权限
	 * @param model
	 * @param Id
	 * @return
	 */
	@RequestMapping("rel_del_right")
	@ResponseBody
	public Map<String, Object>  relDelRight(Model model, String id){
		try{
			if(!StringUtils.hasText(id))return jsonFailResult("操作失败，权限id不能为空！");
			authRightMgrService.changeRightStatus(id, ENTITY_STATUS.REL_DELETE);
			busiLog("权限管理", "物理删除权限", SUCCESSFAIL.SUCCESS,null);
			return jsonSuccessResult("物理删除权限成功");
		} catch (NiceException e) {
			 busiLog("权限管理", "物理删除权限", SUCCESSFAIL.FAIL,e.getMessage());
			return jsonFailResult(e.getMessage());
		}catch (ServiceException e) {
			return jsonFailResult("物理删除权限失败");
		}
	}
	
	/**
	 * 添加权限-跳转
	 * @param model
	 * @return
	 */
	@RequestMapping(value="right_add")
	public String rightAdd(Model model){
		try{
			model.addAttribute("resourceData", authRightMgrService.getMenuByRightId(null,OPERATION_TYPE.ADD));
			return "pub/sysmgr/authmgr/auth_right_add";
		} catch (Exception e) {
			return toAlertPage(model, "获取资源失败");
		}
	}
	/**
	 * 添加权限-提交
	 * @param model
	 * @param authRight
	 * @param menuIds
	 * @return
	 * @throws NiceException 
	 */
	@RequestMapping(value="add_right")
	@ResponseBody
	public Map<String, Object> addRight(Model model,AuthRight authRight,String[] menuIds) {
		
		try{
			authRightMgrService.addRight(authRight,menuIds);
			
			busiLog("权限管理", "添加权限", SUCCESSFAIL.SUCCESS,null);
			return jsonSuccessResult("添加权限成功");
		} catch (ServiceException e) {
			 busiLog("权限管理", "添加权限", SUCCESSFAIL.FAIL,e.getMessage());
			return jsonFailResult("添加权限失败");
		}
	}
	@RequestMapping(value="right_update")
	public String rightUpdate(Model model,String id){
		try{
			AuthRight authRight = authRightMgrService.getRight(id);
			model.addAttribute("authRight", authRight);
			model.addAttribute("resourceData", authRightMgrService.getMenuByRightId(id,OPERATION_TYPE.UPDATE));
			return "pub/sysmgr/authmgr/auth_right_update";
		} catch (ServiceException e) {
			return toAlertPage(model, "获取资源失败");
		}
	}
	/**
	 * 提交修改
	 * @param model
	 * @param authRight
	 * @param resources
	 * @return
	 */
	@RequestMapping(value="update_right")
	@ResponseBody
	public Map<String, Object> updateRight(Model model, AuthRight authRight,String[] menuIds){
		try{
			authRightMgrService.updateRight(authRight,menuIds);
		    busiLog("权限管理", "修改权限", SUCCESSFAIL.SUCCESS,null);
			return jsonSuccessResult("修改权限成功");
		} catch (ServiceException e) {
			busiLog("权限管理", "修改权限", SUCCESSFAIL.FAIL,e.getMessage());
			return jsonFailResult("修改权限失败");
		}
	}
	@RequestMapping(value="get_right_menu")
	public String resourceTreeData(Model model, String rightId){
		try{
			model.addAttribute("menus", authRightMgrService.getMenuByRightId(rightId,OPERATION_TYPE.GET));
			return "pub/sysmgr/authmgr/right_menus";
		} catch (ServiceException e) {
			return toAlertPage(model, "查看资源失败");
		}
	}
	
	
	/**
	 * 暂停权限
	 * @param id
	 * @return
	 */
	@RequestMapping("disable_right")
	@ResponseBody
	public Map<String, Object>  disableRight(String id){
		try{
			 authRightMgrService.changeRightStatus(id,ENTITY_STATUS.DISABLE);
			 busiLog("权限管理", "暂停权限", SUCCESSFAIL.SUCCESS,null);
			 return jsonSuccessResult("暂停角色成功");
		} catch (NiceException e) {
			 busiLog("权限管理", "暂停权限", SUCCESSFAIL.FAIL,e.getMessage());
			return jsonFailResult(e.getMessage());
		}catch (ServiceException e) {
		    busiLog("权限管理", "暂停权限", SUCCESSFAIL.FAIL,e.getMessage());
			return jsonFailResult("暂停权限失败");
		}
	}
	/**
	 * 开启权限
	 * @param id
	 * @return
	 */
	@RequestMapping("enable_right")
	@ResponseBody
	public Map<String, Object>  enableRight(String id){
		try{
			 authRightMgrService.changeRightStatus(id,ENTITY_STATUS.ENABLE);
			 busiLog("权限管理", "开启权限", SUCCESSFAIL.SUCCESS,null);
			 return jsonSuccessResult("开启权限成功");
		} catch (NiceException e) {
			 busiLog("权限管理", "开启角色", SUCCESSFAIL.FAIL,e.getMessage());
			return jsonFailResult(e.getMessage());
		}catch (ServiceException e) {
			 busiLog("权限管理", "开启权限", SUCCESSFAIL.FAIL,e.getMessage());
			return jsonFailResult("开启权限失败");
		}
	}
	/**
	 * 验证权限名称是否已经存在
	 * @param newValue
	 * @param oldValue
	 * @return
	 */
	@RequestMapping("check_right_name")
	@ResponseBody
	public String checkRightCode(String newValue,String oldValue){
		try {
			if (authRightMgrService.rightNameCheck(newValue,oldValue)) {
				return FORM_CHECK_FAIL;
			} else {
				return FORM_CHECK_SUCCESS;
			}
		} catch (ServiceException e) {
			return FORM_CHECK_FAIL;
		}
	}
	
	
}
